﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBaoHanh.Template;
using BussinessLayer;
using QLBaoHanh.DoiBHSuaChua;
using DataAccessLayer;

namespace QLBaoHanh.XuLyBaoHanhSuaChua
{
    public partial class frmDsXuLyBHSC : Form
    {
        private int NhanVienId = 1;
        private IEnumerable<View_ThietBiBHDetail> _dsThietBiBHSC;
        public IEnumerable<View_ThietBiBHDetail> DsThietBiBHSC
        {
            get { return _dsThietBiBHSC; }
            set 
            { 
                _dsThietBiBHSC = value;
                //reset binding
                LoadDataToGrid();
            }
        }


        public frmDsXuLyBHSC()
        {
            InitializeComponent();
            cboFilter.SelectedIndex = 2;
            cboTrangThaiTBBH.SelectedIndex = 0;
        }

        #region Methods

        /// <summary>
        /// Refesh form (grid)
        /// </summary>
        public void RefreshForm()
        {
            GetDataWithCurrentFilter();
        }

        /// <summary>
        /// Get selected item of grid view
        /// </summary>
        /// <returns>0 - not selected else return id</returns>
        private int getSelectedId()
        {
            int Id = 0 ;
            if (dataGridView1.SelectedRows.Count != 0)
            {
                Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
            }
            return Id;
        }

        /// <summary>
        /// Load data to grid
        /// </summary>
        private void LoadDataToGrid()
        {
            int CurrentRowIndex = 0;
            if (dataGridView1.CurrentRow != null)
            {
                CurrentRowIndex = dataGridView1.CurrentRow.Index;
            }
            //getData with current filter
            //GetDataWithCurrentFilter();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = _dsThietBiBHSC;
            //if current row index !=0 then selected it
            if (CurrentRowIndex != 0 && CurrentRowIndex < dataGridView1.Rows.Count )
            {
                this.dataGridView1.Rows[CurrentRowIndex].Selected = true;
            }
            //else Selected to first items.
            if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count == 0)
            {
                this.dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
            }
            int Phien_ThietBiBHIdSelected = getSelectedId();
            LoadGridNhatKiBaoHanh(Phien_ThietBiBHIdSelected);
            //LoadGridCacLanGoiDien(Phien_ThietBiBHIdSelected);
            //LoadGridGhiChu(Phien_ThietBiBHIdSelected);
        }
      
        public void GetDataWithCurrentFilter()
        {
            string option = cboFilter.SelectedItem.ToString();
            DateTime FromDate = dtpFilter.Value;
            DateTime ToDate = dtpToDate.Value;
            string TrangThai = cboTrangThaiTBBH.SelectedItem.ToString();  // dang thuc hien, da hoan thanh
            switch (option)
            {
                case "Ngày":
                    //Loc data trong ngay 
                    if (TrangThai == "Đã hoàn thành") //TRang thai da hoan thanh
                    {
                        DsThietBiBHSC = NghiepVuBO.getInstance.GetDsThietBiBHSCDaHoanThanhByNhanVienIdInDay(NhanVienId,  FromDate);
                    }
                    else
                    {
                        DsThietBiBHSC = NghiepVuBO.getInstance.GetDsThietBiBHSCDangThucHienByNhanVienIdInDay(NhanVienId, FromDate);
                    }
                    break;
                case "Tháng":
                    //Loc data trong thang
                    if (TrangThai == "Đã hoàn thành") //TRang thai da hoan thanh
                    {
                        DsThietBiBHSC = NghiepVuBO.getInstance.GetDsThietBiBHSCDaHoanThanhByNhanVienIdInMonth(NhanVienId, FromDate);
                    }
                    else
                    {
                        DsThietBiBHSC = NghiepVuBO.getInstance.GetDsThietBiBHSCDangThucHienByNhanVienIdInMonth(NhanVienId, FromDate);
                    }
                    break;
                case "Từ ngày":
                    if (TrangThai == "Đã hoàn thành") //TRang thai da hoan thanh
                    {
                        DsThietBiBHSC = NghiepVuBO.getInstance.GetDsThietBiBHSCDaHoanThanhByNhanVienIdBetween(NhanVienId, FromDate, ToDate);
                    }
                    else
                    {
                        DsThietBiBHSC = NghiepVuBO.getInstance.GetDsThietBiBHSCDangThucHienByNhanVienIdBetween(NhanVienId, FromDate, ToDate);
                    }
                    break;
                default:
                    //Get current day
                    DsThietBiBHSC = NghiepVuBO.getInstance.GetDsThietBiBHSCDangThucHienByNhanVienIdBetween(NhanVienId, FromDate, ToDate);
                    break;
            }
        }

        public void LoadGridNhatKiBaoHanh(int inPhien_ThietBiBHId)
        {
            dgvNhatKiBHSC.AutoGenerateColumns = false;
            if (inPhien_ThietBiBHId != 0)
            {
                dgvNhatKiBHSC.DataSource = LichSuBHBO.getInstance.GetLichSuBHByPhien_ThietBiId(inPhien_ThietBiBHId, MyCommon.LoaiLichSu.LichSuSuaChua);
            }
        }

        public void LoadGridCacLanGoiDien(int inPhien_ThietBiBHId)
        {
            dgvCacLanGoiDien.AutoGenerateColumns = false;
            if (inPhien_ThietBiBHId != 0)
            {
                dgvCacLanGoiDien.DataSource = LichSuBHBO.getInstance.GetLichSuBHByPhien_ThietBiId(inPhien_ThietBiBHId, MyCommon.LoaiLichSu.LichSuGoiDien);
            }
        }

        public void LoadGridGhiChu(int inPhien_ThietBiBHId)
        {
            dgvLuuY.AutoGenerateColumns = false;
            if (inPhien_ThietBiBHId != 0)
            {
                dgvLuuY.DataSource = LichSuBHBO.getInstance.GetLichSuBHByPhien_ThietBiId(inPhien_ThietBiBHId, MyCommon.LoaiLichSu.LichSuComment);
            }
        }
#endregion

        #region Events

        private void frmDsXuLyBHSC_Load(object sender, EventArgs e)
        {
            GetDataWithCurrentFilter();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int rowSelected = e.RowIndex;
                if (e.RowIndex != -1)
                {
                    this.dataGridView1.Rows[rowSelected].Selected = true;
                }
            }
        }
        
        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            // nothing is selected
            if (dataGridView1.SelectedRows.Count == 0)
                return;

            if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
            }
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((e.Value is KhachHang) && (e.ColumnIndex == 6)) //TenKhachHang
            {
                e.Value = (e.Value as KhachHang).Ten;
            }
            else if ((e.Value is KhachHang) && (e.ColumnIndex == 7)) //SDT
            {
                e.Value = (e.Value as KhachHang).Mobile;
            }
            else if (e.Value is NhanVien)
            {
                e.Value = (e.Value as NhanVien).Ten;
            }
            else if (e.Value is TrangThaiPhien)
            {
                if ((e.Value as TrangThaiPhien).Id == (int)MyCommon.TrangThaiPhien.ThaoTacNgay_HoanThanh)
                {
                    e.Value = true;
                }
                else
                {
                    e.Value = false;
                }
                
            }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            int PhienId = getSelectedId();
            LoadGridNhatKiBaoHanh(PhienId);
            LoadGridCacLanGoiDien(PhienId);
            LoadGridGhiChu(PhienId);
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            GetDataWithCurrentFilter();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnHoanThanh_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count != 0)
            {
                int Phien_ThietBiBHId = 0;
                string MaThietBi = "";
                Phien_ThietBiBHId = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                MaThietBi = dataGridView1.SelectedRows[0].Cells["MaThietBiBH"].Value.ToString();
                frmHoanThanhBHSC objFrm = new frmHoanThanhBHSC(Phien_ThietBiBHId, MaThietBi, this);
                objFrm.ShowDialog();
            }
        }

        private void cboFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Drawing.Point chkPoint = chkPoint = new System.Drawing.Point(0, 11);
            string option = cboFilter.SelectedItem.ToString();
            switch (option)
            {
                case "Ngày":
                    dtpFilter.Value = DateTime.Now;
                    dtpFilter.Visible = true;
                    dtpFilter.CustomFormat = "dd/MM/yyyy";
                    dtpToDate.Visible = false;
                    lblToDate.Visible = false;
                    chkPoint.X = dtpFilter.Location.X + dtpFilter.Size.Width + 10;
                    this.btnGetData.Location = chkPoint;
                    break;
                case "Tháng":
                    dtpFilter.Value = DateTime.Now;
                    dtpFilter.Visible = true;
                    dtpFilter.CustomFormat = "MM/yyyy";
                    dtpToDate.Visible = false;
                    lblToDate.Visible = false;
                    chkPoint.X = dtpFilter.Location.X + dtpFilter.Size.Width + 10;
                    this.btnGetData.Location = chkPoint;
                    break;
                case "Từ ngày":
                    dtpFilter.Value = DateTime.Now.AddDays(-15);
                    dtpFilter.Visible = true;
                    dtpFilter.CustomFormat = "dd/MM/yyyy";
                    dtpToDate.Visible = true;
                    dtpToDate.CustomFormat = "dd/MM/yyyy";
                    lblToDate.Visible = true;
                    chkPoint.X = dtpFilter.Location.X + dtpFilter.Size.Width + dtpToDate.Size.Width + 70;
                    this.btnGetData.Location = chkPoint;
                    break;
                default:
                    dtpFilter.Value = DateTime.Now;
                    dtpFilter.Visible = true;
                    dtpFilter.CustomFormat = "dd/MM/yyyy";
                    dtpToDate.Visible = false;
                    lblToDate.Visible = false;
                    chkPoint.X = dtpFilter.Location.X + dtpFilter.Size.Width + 10;
                    this.btnGetData.Location = chkPoint;
                    break;
            }
        }

        private void cboTrangThaiTBBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetDataWithCurrentFilter();
        }


        ///////////////////////////////////////////////////////////////////////////
        // Nhat ki

        private void dgvNhatKiBHSC_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((e.Value is NhanVien) && (e.ColumnIndex == 2)) //Ma nhan vien
            {
                e.Value = (e.Value as NhanVien).Ma;
            }
            else if ((e.Value is NhanVien) && (e.ColumnIndex == 3)) //TenNV
            {
                e.Value = (e.Value as NhanVien).Ten;
            }
        }
        
        private void btnAddNhatKi_Click(object sender, EventArgs e)
        {
            int Phien_ThietBiBHId = 0;
            Phien_ThietBiBHId = getSelectedId();
            frmLuuNhatKi frmObj = new frmLuuNhatKi(false, Phien_ThietBiBHId, this, 0);
            frmObj.ShowDialog();
        }

        private void btnEditNhatKi_Click(object sender, EventArgs e)
        {
            int Phien_ThietBiBHId = 0;
            int LichSuId = 0;
            if (dgvNhatKiBHSC.SelectedRows.Count != 0)
            {
                LichSuId = (int)dgvNhatKiBHSC.SelectedRows[0].Cells["LichSuId"].Value;
            }
            else
            {
                return;
            }
            //Neu la lich su nguoi khac thi khong duoc sua
            if (!LichSuBHBO.getInstance.IsCoQuyen(LichSuId, frmMain.CurrentNhanVien.Id).RetValue)
            {
                MessageBox.Show("Không thể sửa nhật ký bảo hành của nhân viên khác", "Sửa nhật kí bảo hành", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Phien_ThietBiBHId = getSelectedId();
            frmLuuNhatKi frmObj = new frmLuuNhatKi(true, Phien_ThietBiBHId, this, LichSuId);
            frmObj.ShowDialog();
        }

        private void btnXoaNhatKi_Click(object sender, EventArgs e)
        {
            if (dgvNhatKiBHSC.SelectedRows.Count != 0)
            {
                int LichSuId = (int)dgvNhatKiBHSC.SelectedRows[0].Cells["LichSuId"].Value;
                //Neu la lich su nguoi khac thi khong duoc sua
                if (!LichSuBHBO.getInstance.IsCoQuyen(LichSuId, frmMain.CurrentNhanVien.Id).RetValue)
                {
                    MessageBox.Show("Không thể xóa nhật ký bảo hành của nhân viên khác", "Xóa nhật kí bảo hành", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                
                if (MessageBox.Show("Bạn chắc chắn muốn xóa nhật kí bảo hành, sửa chữa?"
                    , "Nhật kí bảo hành sửa chữa", MessageBoxButtons.YesNo
                    , MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    //Tru chi phi sua chua cua thiet bi khi xoa ban ghi nay
                    LichSuBH aLichSu = LichSuBHBO.getInstance.GetLichSuBHById(LichSuId);
                    if (aLichSu.ChiPhi > 0)
                    {
                        Phien_ThietBiBH aPhien_TB = Phien_ThietBiBHBO.getInstance.GetPhien_ThietBiBHById((int)aLichSu.Phien_ThietBiBHId);
                        aPhien_TB.ChiPhi = aPhien_TB.ChiPhi - aLichSu.ChiPhi;
                        Phien_ThietBiBHBO.getInstance.UpdatePhien_ThietBiBH(aPhien_TB);
                    }
                    LichSuBHBO.getInstance.DelLichSuBH(LichSuId);
                    //Load lai grid nay
                    LoadGridNhatKiBaoHanh(getSelectedId());
                }
            }
        }


        ///////////////////////////////////////////////////////////////////////////
        //Luu y

        private void dgvLuuY_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((e.Value is NhanVien) && (e.ColumnIndex == 2)) //Ma nhan vien
            {
                e.Value = (e.Value as NhanVien).Ma;
            }
            else if ((e.Value is NhanVien) && (e.ColumnIndex == 3)) //TenNV
            {
                e.Value = (e.Value as NhanVien).Ten;
            }
        }

        private void btnAddLuuY_Click(object sender, EventArgs e)
        {
            int Phien_ThietBiBHId = 0;
            Phien_ThietBiBHId = getSelectedId();
            frmThemLuuY frmObj = new frmThemLuuY(false, Phien_ThietBiBHId, this, 0);
            frmObj.ShowDialog();
        }

        private void btnEditLuuY_Click(object sender, EventArgs e)
        {
            int Phien_ThietBiBHId = 0;
            int LichSuId = 0;
            if (dgvLuuY.SelectedRows.Count != 0)
            {
                LichSuId = (int)dgvLuuY.SelectedRows[0].Cells["LuuYId"].Value;
            }
            else
            {
                return;
            }
            //Neu la lich su nguoi khac thi khong duoc sua
            if (!LichSuBHBO.getInstance.IsCoQuyen(LichSuId, frmMain.CurrentNhanVien.Id).RetValue)
            {
                MessageBox.Show("Không thể sửa lưu ý của nhân viên khác", "Sửa lưu ý", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Phien_ThietBiBHId = getSelectedId();
            frmThemLuuY frmObj = new frmThemLuuY(true, Phien_ThietBiBHId, this, LichSuId);
            frmObj.ShowDialog();
        }

        private void btnXoaLuuY_Click(object sender, EventArgs e)
        {
            int LichSuId = (int)dgvLuuY.SelectedRows[0].Cells["LuuYId"].Value;
            //Neu la lich su nguoi khac thi khong duoc sua
            if (!LichSuBHBO.getInstance.IsCoQuyen(LichSuId, frmMain.CurrentNhanVien.Id).RetValue)
            {
                MessageBox.Show("Không thể xóa lưu ý của nhân viên khác", "Xóa lưu ý", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (dgvLuuY.SelectedRows.Count != 0)
            {
                //TODO: confirm
                if (MessageBox.Show("Bạn chắc chắn muốn xóa nhật kí bảo hành, sửa chữa?"
                    , "Nhật kí bảo hành sửa chữa", MessageBoxButtons.YesNo
                    , MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    //Tru chi phi sua chua cua thiet bi khi xoa ban ghi nay
                    LichSuBH aLichSu = LichSuBHBO.getInstance.GetLichSuBHById(LichSuId);
                    LichSuBHBO.getInstance.DelLichSuBH(LichSuId);
                    //Load lai grid nay
                    LoadGridGhiChu(getSelectedId());
                }
            }
        }

        private void btnLuuY_Click(object sender, EventArgs e)
        {
            int Phien_ThietBiBHId = 0;
            Phien_ThietBiBHId = getSelectedId();
            frmThemLuuY frmObj = new frmThemLuuY(false, Phien_ThietBiBHId, this, 0);
            frmObj.ShowDialog();
        }
        
        
        ////////////////////////////////////////////////////////////////////////////
        // Goi dien

        private void dgvCacLanGoiDien_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((e.Value is NhanVien) && (e.ColumnIndex == 2)) //Ma nhan vien
            {
                e.Value = (e.Value as NhanVien).Ma;
            }
            else if ((e.Value is NhanVien) && (e.ColumnIndex == 3)) //TenNV
            {
                e.Value = (e.Value as NhanVien).Ten;
            }
        }

        private void btnGoiDien_Click(object sender, EventArgs e)
        {
            int Phien_ThietBiBHId = 0;
            Phien_ThietBiBHId = getSelectedId();
            frmThemGoiDien frmObj = new frmThemGoiDien(false, Phien_ThietBiBHId, this, 0);
            frmObj.ShowDialog();
        }
        
        private void btnAddGoiDien_Click(object sender, EventArgs e)
        {
            int Phien_ThietBiBHId = 0;
            Phien_ThietBiBHId = getSelectedId();
            frmThemGoiDien frmObj = new frmThemGoiDien(false, Phien_ThietBiBHId, this, 0);
            frmObj.ShowDialog();
        }

        private void btnEditGoiDien_Click(object sender, EventArgs e)
        {
            int Phien_ThietBiBHId = 0;
            int LichSuId = 0;
            if (dgvCacLanGoiDien.SelectedRows.Count != 0)
            {
                LichSuId = (int)dgvCacLanGoiDien.SelectedRows[0].Cells["GoiDienId"].Value;
            }
            else
            {
                return;
            }
            //Neu la lich su nguoi khac thi khong duoc sua
            if (!LichSuBHBO.getInstance.IsCoQuyen(LichSuId, frmMain.CurrentNhanVien.Id).RetValue)
            {
                MessageBox.Show("Không thể sửa lần gọi điện của nhân viên khác", "Sửa lần gọi điện", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Phien_ThietBiBHId = getSelectedId();
            frmThemGoiDien frmObj = new frmThemGoiDien(true, Phien_ThietBiBHId, this, LichSuId);
            frmObj.ShowDialog();
        }

        private void btnDelGoiDien_Click(object sender, EventArgs e)
        {
            if (dgvCacLanGoiDien.SelectedRows.Count != 0)
            {
                int LichSuId = (int)dgvCacLanGoiDien.SelectedRows[0].Cells["GoiDienId"].Value;
                //Neu la lich su nguoi khac thi khong duoc sua
                if (!LichSuBHBO.getInstance.IsCoQuyen(LichSuId, frmMain.CurrentNhanVien.Id).RetValue)
                {
                    MessageBox.Show("Không thể xóa lần gọi điện của nhân viên khác", "Xóa lần gọi điện", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (MessageBox.Show("Bạn chắc chắn muốn xóa lịch sử gọi điện?"
                    , "Lịch sử gọi điện", MessageBoxButtons.YesNo
                    , MessageBoxIcon.Information) == DialogResult.Yes)
                {
                
                    //Tru chi phi sua chua cua thiet bi khi xoa ban ghi nay
                    LichSuBH aLichSu = LichSuBHBO.getInstance.GetLichSuBHById(LichSuId);
                    LichSuBHBO.getInstance.DelLichSuBH(LichSuId);
                    //Load lai grid nay
                    LoadGridCacLanGoiDien(getSelectedId());
                }
            }
        }
        
        #endregion
    }
}
